<?php
class console_ctl_admin_delivery_sync extends desktop_controller {

    var $name = "通知仓库取消失败列表";
    var $workground = "console_center";

    /**
     *
     * 发货单列表
     */
    function index(){

        $user = kernel::single('desktop_user');
        
        $actions = array();
       
       $base_filter = array(
            'type' => 'normal',
            'pause' => 'false',
            'parent_id' => 0,
            'disabled' => 'false',
            'sync' => array('fail','succ'),
        );
        $base_filter = array_merge($base_filter,$_GET);
        switch ($_GET['view']) {
            case '1':
                $actions[] =  array('label' => '重试取消发货单', 'submit' => 'index.php?app=console&ctl=admin_delivery_sync&act=batch_sync', 'confirm' => '你确定要对勾选的发货单进行发货取消吗？', 'target' => 'refresh');

                //$actions[] = array('label' => '发货单取消', 'submit' => 'index.php?app=console&ctl=admin_delivery_sync&act=batch_cancel', 'confirm' => "这些发货单认为都是在仓储已经取消发货，请确认这些发货单WMS已经取消！！！\n\n警告：本操作将会直接取消发货单并释放库存，并不可恢复，请谨慎使用！！！", 'target' => 'refresh');
                break;
        }
        
        /*
        if ($_GET['view'] == '1') {
            $sync_status = 'fail';
        }elseif($_GET['view'] == '2'){
            $sync_status = 'succ';
        }
        $actions[] =  array(
            'label'=>'导出',
            'submit'=>'index.php?app=omedlyexport&ctl=ome_delivery&act=index&action=export&status=cancel&sync='.$sync_status,
            'target'=>'dialog::{width:400,height:170,title:\'导出\'}'
        );
        */
        $params = array(
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_import'=>false,
            'use_buildin_export'=>false,
            'use_buildin_filter'=>true,
            'use_view_tab'=>true,
            'actions' => $actions,
            'title'=>'通知仓库取消失败列表',
            'base_filter' => $base_filter,
        );

        
        $this->finder('console_mdl_delivery', $params);
    }

    function _views(){
        $oDelivery = app::get('console')->model('delivery');
        $base_filter = array(
            'type' => 'normal',
            'pause' => 'false',
            'parent_id' => 0,
            'disabled' => 'false',
        );
        $sub_menu = array(
            0 => array('label'=>app::get('base')->_('全部'),'filter'=>array('sync' => array('fail','succ')),'optional'=>false),
            1 => array('label'=>app::get('base')->_('失败'),'filter'=>array('sync' => 'fail'),'optional'=>false),
            2 => array('label'=>app::get('base')->_('成功'),'filter'=>array('sync' => 'succ'),'optional'=>false),
           
        );
        foreach($sub_menu as $k=>$v){
            if (!IS_NULL($v['filter'])){
                $v['filter'] = array_merge($v['filter'], $base_filter);
            }
            
            $sub_menu[$k]['filter'] = $v['filter']?$v['filter']:null;
            $sub_menu[$k]['addon'] = $oDelivery->viewcount($v['filter']);
            $sub_menu[$k]['href'] = 'index.php?app=console&ctl='.$_GET['ctl'].'&act='.$_GET['act'].'&view='.$i++;
        }

        return $sub_menu;
    }

    
    /**
     * 批量同步.
     * @param 
     * @return 
     * @access  public
     * @author sunjing@shopex.cn
     */
    function batch_sync()
    {
        $this->begin('');
        kernel::database()->exec('commit');
        
        $ids = $_REQUEST['delivery_id'];
        if (!empty($ids)) {
            $delivery_list = kernel::database()->select("SELECT delivery_id,branch_id,delivery_bn from sdb_ome_delivery where delivery_id in (" . join(',', $ids) . ") and sync in ('fail')");
            foreach ((array) $delivery_list as $delivery) {
                $res = ome_delivery_notice::cancel($delivery, true);
                if ($res['rsp'] == 'fail') {
                    $oOperation_log->write_log('delivery_back@ome',$delivery_id,'发货单取消通知仓库:失败,原因'.$res['msg']);
                }else{
                    $deliveryObj = app::get('ome')->model('delivery');
                    $deliveryObj->update_sync_cancel($delivery_id,'succ');
                    $oOperation_log->write_log('delivery_back@ome',$delivery_id,'发货单取消通知仓库:成功');
                }
            }
        }

        $this->end(true, '命令已经被成功发送！！');
    }

    
    /**
     * 批量取消.
     * @param   
     * @return  
     * @access  public
     * @author sunjing@shopex.cn
     */
    function batch_cancel()
    {
        $this->begin('');
        kernel::database()->exec('commit');
        $ids = $_REQUEST['delivery_id'];
        $branchLib = kernel::single('ome_branch');
        $oOperation_log = app::get('ome')->model('operation_log');
        $deliveryObj = app::get('ome')->model('delivery');
        if (!empty($ids)) {

            $delivery_list = kernel::database()->select("SELECT delivery_id,branch_id,delivery_bn from sdb_ome_delivery where delivery_id in (" . join(',', $ids) . ") and sync in ('fail') AND status in('ready','progress')");
            
            foreach ((array) $delivery_list as $delivery) {
                $data = array(
                    'status'=>'cancel',
                    'memo'=>'发货单请求第三方仓储取消失败,强制取消!',
                    'delivery_bn'=>$delivery['delivery_bn'],
                );
                kernel::single('ome_event_receive_delivery')->update($data);
            }
        }
        $this->end(true, '命令已经被成功发送！！');
    }
   
}
